<template>
  <div class="main">
    <el-button type="primary" size="mini" class="add-btn" @click="addType">添加分类</el-button>
    <el-table :data="typeList" border style="width: 100%">
      <el-table-column prop="typeId" label="编号" />
      <el-table-column prop="typeName" label="分类" />
      <el-table-column prop="typeBlogCount" label="博客数量" />
      <el-table-column prop="enable" label="启用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable === 1">启用</el-tag>
          <el-tag v-else type="info">未启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.typeId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.typeId)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.enable === 0">
                <el-button size="mini" type="success" @click="handleEnable(scope.row.typeId)">启用</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-else>
                <el-button size="mini" type="info" @click="handleDisable(scope.row.typeId)">弃用</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      title="添加分类"
      :visible.sync="drawer"
      :direction="direction"
    >
      <type-add ref="add" @getTypeList="getTypeList" @closeAddTypeForm="closeAddTypeForm" />
    </el-drawer>

    <el-dialog
      title="修改分类"
      :visible.sync="showUpdate"
      width="30%"
    >
      <type-update :type="type" @getTypeList="getTypeList" @closeUpdateForm="closeUpdateForm" />
    </el-dialog>
  </div>
</template>

<script>
import typeApi from '@/api/type'
import TypeAdd from './type-add'
import TypeUpdate from './type-update'
export default {
  components: {
    TypeAdd,
    TypeUpdate
  },
  data() {
    return {
      drawer: false, // 控制新增分类窗体是否打开
      direction: 'rtl', // 控制分类窗体打开方向
      showUpdate: false, // 控制分类修改弹窗是否打开
      typeList: [], // 分类数组
      type: {}
    }
  },
  created() {
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      // 查询分类列表
      typeApi.listBack().then(resp => {
        console.log(resp.data)
        this.typeList = resp.data
        typeApi.getList().then(resp => {
          // 更新全局变量
          this.$store.commit('global/SET_TYPE', resp.data)
        })
      })
    },
    handleEdit(typeId) {
      // 修改
      this.showUpdate = true
      typeApi.getById(typeId).then(resp => {
        this.type = resp.data
      })
    },
    handleDelete(typeId) {
      // 删除
      this.$confirm('是否删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        typeApi.deleteById(typeId)
        this.$message.success('删除成功!')
        this.getTypeList()
      })
    },
    handleEnable(typeId) {
      this.$confirm('是否启用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        typeApi.enableById(typeId).then(resp => {
          this.$message.success('启用成功!')
          this.getTypeList()
        })
      })
    },
    handleDisable(typeId) {
      this.$confirm('是否弃用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        typeApi.disableById(typeId).then(resp => {
          this.$message.success('弃用成功!')
          this.getTypeList()
        })
      })
    },
    addType() {
      this.drawer = true
      // this.$refs.add.init()
    },
    closeAddTypeForm() {
      this.drawer = false
    },
    closeUpdateForm() {
      this.showUpdate = false
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 0px 5px 0px;
  }
</style>
